class DevicesController < ApplicationController
  
  def create
    @device = Device.new(:phone_number => params[:number],
                            :user_id => params[:user],
                            :active => 1,
                            :status => 'Enabled')
    if @device.save
      rtn_json({'id'=>@device.id, 'status' => '200'})
    else
      rtn_json({'error' => 'Could not create a device'})
    end
  end
  
  def deactivate
    @device = Device.find(params[:id])
    @device.update_attributes(:active => 0)
    @device.update_attributes(:status => 'Disabled')
    rtn_json({'status' => '200'})
  end
  
  def activate
    @device = Device.find(params[:id])
    @device.update_attributes(:active => 1)
    @device.update_attributes(:status => 'Enabled')
    rtn_json({'status' => '200'})
  end
  
  def status
    @device = Device.find(params[:id])
    
    rtn_json({'status' => '200', 'device' => @device.status})
  end
  
  def rtn_json(obj)
    respond_to do |format|
      format.html{render json: obj, :callback => params[:callback]}
      format.json{render json: obj, :callback => params[:callback]}
    end
  end
end
